package com.txby.hisJob.config;


import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

/**
 * Created by summer on 2016/11/25.
 */
@Configuration
@MapperScan(basePackages = { 
"com.txby.platformApi.component.service" }, sqlSessionTemplateRef = "sqlSessionHis")
public class HisDataSourceConfig  {
//	private String typeAliasPackage = "com.txby.entity";
	/** * mybatis 配置路径 */ 
//	private static String MYBATIS_CONFIG = "mybatis/mybatis-config.xml";
//	
//	@Qualifier("hisDataSource") DataSource dataSource;
	
	@Bean(name = "sqlSessionFactoryHis")
	public SqlSessionFactoryBean sqlServerSqlSessionFactory(@Qualifier("hisDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
//      bean.setTypeAliasesPackage(typeAliasPackage);
//		bean.setConfigLocation(new ClassPathResource("MYBATIS_CONFIG"));
//		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapping/*.xml"));
		return bean;
	}

	@Bean(name = "sqlSessionHis")
	public SqlSessionTemplate sqlSession(@Qualifier("sqlSessionFactoryHis") SqlSessionFactory sqlSessionFactory)
			throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	//分布式事务不能配置自由的事务管理器,否则分布式事务管理器无法起作用
	/* @Bean(name = "transactionManagerHis")
    @Primary
	public DataSourceTransactionManager transactionManagerHis(@Qualifier("hisDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	} */

}
